import { FormSchema } from '/@/components/Table';

import { receivedList, getAll as getAllContract } from '/@/api/business/contract';
import { getBank } from '/@/api/business/contract';




function addPrefix(obj, prefix) {
  return Object.keys(obj).reduce((acc, key) => {
    acc[`${prefix}${key}`] = obj[key];
    return acc;
  }, {});
}

/**
 * 表单字段定义
*/
export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'formNo',
    label: '表单编号',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'contractId',
    label: '所属合同',
    required: true,
    component: 'ApiSelect',
    componentProps: ({ formActionType }) => ({
      api: getAllContract,
      showSearch: true, 
      optionFilterProp: 'label',
      resultField: 'data',
      labelField: 'formNo',
      valueField: 'id',
      onChange: (id, contractData) => {
        console.log('所属合同更新, 合同id=', id, contractData)
        
        //updateSchema 变更其它选择项信息
        const { updateSchema, setFieldsValue } = formActionType
        //setFieldsValue({ 'receivedId': '' })
        setFieldsValue({ 'receiptCompany': '' })

        updateSchema({
          field: 'receivedId',
          componentProps: {
            api: receivedList,
            params: { contractId: id },
            mode: "multiple",
            labelField: 'detail',
            valueField: 'id',
          }
        })

        updateSchema({
          field: 'receiptCompany',
          componentProps: {
            onChange: (id, record) => {
              const prefixedObj = addPrefix(record, 'bankInfo.')
              console.log('发票抬头更新', prefixedObj)
              setFieldsValue(prefixedObj)
            },
            api: getBank,
            params: { id: id },
            resultField: 'data',
            labelField: 'title',
            valueField: 'id',
          }
        })
        //
      },
    }),
    //componentProps end
  },


  {
    field: 'receivedId',
    label: '关联合同收款',
    required: true,
    component: 'ApiSelect',
  },

  {
    field: 'receiptAmount',
    label: '开票金额',
    component: 'Input',
    required: true,
  },
  {
    field: 'receiptType',
    label: '发票类型',
    required: true,
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '增值税电子普通发票',
          value: '增值税电子普通发票',
          key: '1',
        },
        {
          label: '增值税电子专用发票',
          value: '增值税电子专用发票',
          key: '2',
        },
        {
          label: '增值税普通发票',
          value: '增值税普通发票',
          key: '3',
        },
        {
          label: '增值税专用发票',
          value: '增值税专用发票',
          key: '4',
        },
      ],
    },
  },
  {
    field: 'receiptCompany',
    label: '发票抬头',
    component: 'ApiSelect',
  },
  {
    field: 'bankInfo.companyNo',
    label: '纳税人识别号',
    component: 'Input',
  },
  {
    field: 'bankInfo.address',
    label: '企业地址',
    component: 'Input',
  },
  {
    field: 'bankInfo.tel',
    label: '联系电话',
    component: 'Input',
  },
  {
    field: 'bankInfo.bankName',
    label: '开户银行',
    component: 'Input',
  },
  {
    field: 'bankInfo.bankNo',
    label: '银行卡号',
    component: 'Input',
  },
  {
    field: 'receiptRemark',
    label: '备注',
    component: 'InputTextArea',
  },

];

